import React, { Fragment } from 'react';
import { formatMessage } from 'umi/locale';
import Link from 'umi/link';
import { Icon } from 'antd';
import GlobalFooter from '@/components/GlobalFooter';
import SelectLang from '@/components/SelectLang';
import styles from './UserLayout.less';
import logo from '../assets/logo.svg';
import { connect } from 'dva';

const copyright = (
  <Fragment>
    Copyright <Icon type="copyright" /> 2021 深圳恒鼎智能装备有限公司 V1.0
  </Fragment>
);
@connect(({ hd }) => ({ hd }))
class UserLayout extends React.PureComponent {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const { dispatch } = this.props;
    if (!window.eel.wsOpened) {
      let page = window.location.pathname.substring(1);
      // eel._position_window(page);

      let websocket_addr = ('http://localhost:9088' + '/eel').replace('http', 'ws');
      websocket_addr += '?page=' + page;
      window.eel._websocket = new WebSocket(websocket_addr);
      window.eel._websocket.onopen = function(e) {
        console.log('op');
        window.eel.wsOpened = true;
      };
      window.eel._websocket.onmessage = function(e) {
        const message = JSON.parse(e.data);
        // console.log("msg",message);
        if (message.hasOwnProperty('deviceStates')) {
          // console.log("states",message);
          dispatch({
            type: 'ds/handleDeviceStatesMessage',
            payload: { msg: message.deviceStates },
          });
          // Python making a function call into us
          // if (message.name in window.eel._exposed_functions) {
          //   let return_val = window.eel._exposed_functions[message.name](...message.args);
          //   window.eel._websocket.send(window.eel._toJSON({ return: message.call, value: return_val }));
          // }
        } else if (message.hasOwnProperty('orderStates')) {
          dispatch({
            type: 'os/handleOrderStateMessage',
            payload: { msg: message.orderStates },
          });
        } else if (message.hasOwnProperty('alarmInfo')) {
          dispatch({
            type: 'ds/handleAlarmInfo',
            payload: { msg: message.alarmInfo },
          });
        } else if (message.hasOwnProperty('rs')) {
          // console.log("states",message);
          dispatch({
            type: 'produce/wsHandleRs',
            payload: { msg: message },
          });
        } else if (message.hasOwnProperty('run')) {
          // console.log("states",message);
          dispatch({
            type: 'produce/wsHandleRun',
            payload: { msg: message },
          });
        } else if (message.hasOwnProperty('return')) {
          // Python returning a value to us
          console.log('recv msg', message);
          if (message.return in window.eel._call_return_callbacks) {
            window.eel._call_return_callbacks[message.return](message.value);
            delete window.eel._call_return_callbacks[message.return];
          }
        } else if (message.hasOwnProperty('produceState')) {
          dispatch({
            type: 'produce/handleProduceMessage',
            payload: { msg: message.produceState },
          });
        } else if (message.hasOwnProperty('settingsUpdate')) {
          dispatch({
            type: 'hdst/handleSettingsUpdate',
          });
        } else if (message.hasOwnProperty('cbjZmcToolState')) {
          dispatch({
            type: 'cbjt/handleCbjToolMsg',
            payload: { msg: message.cbjZmcToolState },
          });
        } else if (message.hasOwnProperty('lineCamCalState')) {
          dispatch({
            type: 'lc/handleLineCamCalMsg',
            payload: { msg: message.lineCamCalState },
          });
        } else if (message.hasOwnProperty('lineCamJointState')) {
          dispatch({
            type: 'cj/handleLineCamJointMsg',
            payload: { msg: message.lineCamJointState },
          });
        } else if (message.hasOwnProperty('cutterCamCalState')) {
          dispatch({
            type: 'ccc/handleCutterCameraCalMsg',
            payload: { msg: message.cutterCamCalState },
          });

        } else if (message.hasOwnProperty('eapMsgState')) {
          dispatch({
            type: 'es/handleEapMsgState',
            payload: { msg: message.eapMsgState },
          });
        } else if (message.hasOwnProperty('cimMsgState')) {
          dispatch({
            type: 'es/handleCimMsgState',
            payload: { msg: message.cimMsgState },
          });
        }else {
          throw `Invalid message ${message}`;
        }
      };
    }
  }
  render() {
    const { children } = this.props;
    return (
      // @TODO <DocumentTitle title={this.getPageTitle()}>
      <div className={styles.container}>
        <div className={styles.lang}>
          <SelectLang />
        </div>
        <div className={styles.content}>
          <div className={styles.top}>
            <div className={styles.header}>
              <h1>请输入用户名和密码</h1>
            </div>
          </div>
          {children}
        </div>
        <GlobalFooter copyright={copyright} />
      </div>
    );
  }
}

export default UserLayout;
